<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/bootstrap-theme.min.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/themes/icon.css"/>

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
	$(function(){
		//隐藏修改框
		$('#modifyDiv').window('close');
		
		$("#dishMsg").datagrid({
			url: '${pageContext.request.contextPath}/showByCartAction',
			title: '购物车',
			striped:true,
			columns:[[ 
					{
					   field: 'dtemp1',
					   title: '菜品样式',
					   width: 160,
					   align:'center',
					   formatter:function(value,row,index){   //关键：格式化，并返回一个img标签
					   return '<img width="60%" height="60px" src="'+row.dtemp1+'"/>'
					   },
					 },     
			        {field:'bid',title:'购物车id',width:100,hidden:true},
			        {field:'sid',title:'商家id',width:100,hidden:true},
			        {field:'did',title:'菜品id',width:100,hidden:true},
			        {field:'dname',title:'菜品名字',width:220},
			        {field:'bnum',title:'菜品数量',width:220},
			        {field:'dprice',title:'菜品价格',width:150}, 
			        {field:'sname',title:'餐厅名字',width:220},
			        {field:'checked',checkbox:true,width:100},
			        
			]],
			//是否显示分页
			pagination:true,
			rownumbers : true,
			//按钮
			toolbar: [{
				iconCls: 'icon-ok',
				text:'生成订单',
				handler: function(){
					var buycarts =	$("#dishMsg").datagrid('getSelections');
					if(buycarts.length<1){
						$.messager.alert('提示','你没有选择');
					}else{
						window.parent.$.messager.confirm('提示','确定要生成订单吗',function(r){    
						    if (r){    
						    	var bids = new Array();
								var sids = new Array();
								var dnames = new Array();
								var bnums = new Array();
								var dprices = new Array();
								var dids = new Array();
								var index = 0;
								for (var i = 0; i < buycarts.length; i++) {
									bids[index]=buycarts[i].bid;
									sids[index]=buycarts[i].sid;
									dnames[index]=buycarts[i].dname;
									bnums[index]=buycarts[i].bnum;
									dprices[index]=buycarts[i].dprice;
									dids[index]=buycarts[i].did;
									index++;
								}
								$.ajax({
									type: 'post',
									url: '${pageContext.request.contextPath}/newOrdertAction',
									data: {"bids":bids.toString(),"sids":sids.toString(),"bnums":bnums.toString(),"dprices":dprices.toString(),"dnames":dnames.toString(),"dids":dids.toString()},
									daType: 'text',
									success: function(data){
										if(data=="0"){
											window.parent.$.messager.alert('提示','对不起你不是选得一家餐厅下单',"error");
										}else if(data=="1"){
											$('#dishMsg').datagrid('reload');
											$.messager.show({
												title:'提示',
												msg:'生成订单成功',
												timeout:2000,
												showType:'slide'
											});
										}else{
											window.parent.$.messager.alert('提示',data,"error");
										}
									}
								});    
						    }    
						});
						
					}
				}
			},'-',{
				iconCls: 'icon-edit',
				text:'修改',
				handler: function(){
					var buycart =	$("#dishMsg").datagrid('getSelections');
					if(buycart.length!=1){
						$.messager.confirm('提示','请选择一条数据',function(){    
							$("#dishMsg").datagrid('clearSelections');
						}); 
					}else{
						var buycart = $("#dishMsg").datagrid('getSelections');
						$("#modifyForm [name=bid]").val(buycart[0].bid);
						$('#modifyDiv').window('open');
					}
				}
			},'-',{
				iconCls: 'icon-remove',
				text:'删除',
				handler: function(){
					var buycarts =	$("#dishMsg").datagrid('getSelections');
					if(buycarts.length<1){
						$.messager.alert('提示','你没有选择');
					}else{
						window.parent.$.messager.confirm('提示','你确定要删除吗',function(r){    
						    if (r){    
						    	var bids = [];
								var index = 0;
								for (var i = 0; i < buycarts.length; i++) {
									bids[index++]=buycarts[i].bid;
								}
								$.ajax({
									type: 'post',
									url: '${pageContext.request.contextPath}/deleteBuyCartAction',
									data: {"bids": bids.toString()},
									dataType: 'text',
									success: function(data){
										if(data=="1"){
											$('#dishMsg').datagrid('reload');
											$.messager.show({
												title:'提示',
												msg:'删除成功',
												timeout:2000,
												showType:'slide'
											});
										}else{
											$.messager.show({
												title:'提示',
												msg:'删除失败',
												timeout:2000,
												showType:'slide'
											});
										}
									}
								});    
						    }    
						});
						
					}
				}
			}]
		});
		
		//配置修改表单信息
		$("#modifyForm").form({
			url:'${pageContext.request.contextPath}/modifyBuyCartAction',
			success:function(data){
				if(data=="1"){
					window.parent.$.messager.confirm('提示','修改成功',function(r){    
					    if (r){    
					    	window.location = "${pageContext.request.contextPath}/buycart/showdishies.jsp"    
					    }    
					});
				}else{
					window.parent.$.messager.alert('提示','修改失败,输入的不是数字',"error");
				}
			}
		});
		
		//配置只能输入数字
		$.extend($.fn.validatebox.defaults.rules, {
			number : {
				validator : function(value) {
					var reg = /^[0-9]*$/;
					return reg.test(value);
				},
				message : '请输入数字'
			},
		});
	});
	function modifyForm(){
		$("#modifyForm").form('submit');
	}
</script>
<body class="container-fluid">
	<a name="top"></a>
	<table id="dishMsg"></table>
	<div id="modifyDiv" class="easyui-dialog" title="修改" style="width:400px;height:300px;" data-options="modal:true">      
		<form id="modifyForm" method="post">
	    	<table>
	    		<tr>
	    			<td>数量</td>
	    			<td><input class="easyui-textbox" type="text" name="bnum" data-options="required:'true',validType:'number'"></td>
	    		</tr>
	    		<tr style="display: none">
	    			<td>购物车菜品id</td>
	    			<td><input class="easyui-textbox" type="text" name="bid"></td>
	    		</tr>
	    	</table>
	    </form>
	    <div style="text-align:center;">
	    	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="modifyForm()" style="margin-top: 20px">确&nbsp;定</a>
	    </div>
	</div>
	<div style="position: fixed;bottom:3%;right: 3%">
		<a href="#top"><span class="glyphicon glyphicon-arrow-up"></span></a>
	</div>
</body>
</html>